package com.book.manager.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.book.entity.Manager;
import com.book.jdbc.JDBC;


public class BookDao {
	public boolean addManager(Manager m) {
		try{
			Connection con=JDBC.getCon();
			PreparedStatement pstm=con.prepareStatement("insert into manager (Mid,Mname,Mnum,Mpassword) values(?,?,?,?)");
			pstm.setInt(1, m.getMid());
			pstm.setString(2, m.getMname());
			pstm.setInt(3, m.getMnum());
			pstm.setString(4, m.getMpassword());
			int rs = pstm.executeUpdate();
			if(rs>0){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean updateManagerById(Manager m){
		try{
			Connection con=JDBC.getCon();
			PreparedStatement pstm=con.prepareStatement("update manager set Mid=?,Mname=?,Mnum=?,Mpassword=? where Mid=?");
			pstm.setInt(1, m.getMid());
			pstm.setString(2, m.getMname());
			pstm.setInt(3, m.getMnum());
			pstm.setString(4, m.getMpassword());
			pstm.setInt(5, m.getMid());
            int rs = pstm.executeUpdate();
            System.out.println(rs);
            if(rs>0){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	public boolean deleteManagerByid(int mid){
		try{
			Connection con=JDBC.getCon();
			PreparedStatement pstm=con.prepareStatement("delete from manager where Mid=?");
			pstm.setInt(1, mid);
			int rs=pstm.executeUpdate();
			if(rs>0){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	public Manager findManagerById(int id){
		try{
			Connection con=JDBC.getCon();
			PreparedStatement pstm=con.prepareStatement("select * from manager where Mid=?");
			pstm.setInt(1, id);
			ResultSet rs=pstm.executeQuery();
			Manager m=null;
			while(rs.next()){
				m=new Manager();
				m.setMid(rs.getInt(1));
				m.setMname(rs.getString(2));
				m.setMnum(rs.getInt(3));
				m.setMpassword(rs.getString(4));
			}
			return m;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	public Manager findManagerByName(String name){
		try{
			Connection con=JDBC.getCon();
			PreparedStatement pstm=con.prepareStatement("select * from manager where Mname=?");
			pstm.setString(1, name);
			ResultSet rs=pstm.executeQuery();
			Manager m=null;
			while(rs.next()){
				m=new Manager();
				m.setMid(rs.getInt(1));
				m.setMname(rs.getString(2));
				m.setMnum(rs.getInt(3));
				m.setMpassword(rs.getString(4));
			}
			return m;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	public List<Manager> findAll(){
		try{
			List<Manager> list=new ArrayList<Manager>();
			Connection con=JDBC.getCon();
			PreparedStatement pstm=con.prepareStatement("select * from manager");
			ResultSet rs=pstm.executeQuery();
			Manager m=null;
			while(rs.next()){
				m=new Manager();
				m.setMid(rs.getInt(1));
				m.setMname(rs.getString(2));
				m.setMnum(rs.getInt(3));
				m.setMpassword(rs.getString(4));
				list.add(m);
				System.out.println(m);
			}
			
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	public boolean isExist(int id){
		return findManagerById(id)==null?false:true;
	}
	public boolean isExist(String name){
		return findManagerByName(name)==null?false:true;
	}
}
